import 'sqflite_helper.dart';

const String _compareTime = 'compareTime';
const String _deviceNo = 'deviceNo';
const String _errorCode = 'errorCode';
const String _faceUrl = 'faceUrl';
const String _cropUrl = 'cropUrl';
const String _score = 'score';
const String _staff_name = 'staff_name';
const String _style = 'style';
const String _size = 'size';

class Compare {
  String deviceNo;
  String compareTime;
  String staff_name;
  double score;
  int errorCode;
  String faceUrl;
  String cropUrl;
  String style;
  int size;

  Compare();

  Map<String, dynamic> toMap() {
    Map<String, dynamic> map = {
      _compareTime: compareTime,
      _deviceNo: deviceNo,
      _errorCode: errorCode,
      _faceUrl: faceUrl,
      _cropUrl: cropUrl,
      _score: score,
      _staff_name: staff_name,
      _style: style,
      _size: size,
    };
    return map;
  }

  Compare.fromMap(Map<String, dynamic> map) {
    compareTime = map[_compareTime];
    deviceNo = map[_deviceNo];
    errorCode = map[_errorCode];
    faceUrl = map[_faceUrl];
    cropUrl = map[_cropUrl];
    score = map[_score];
    staff_name = map[_staff_name];
    style = map[_style];
    size = map[_size];
  }
}

class CompareProvider {
  static CompareProvider _instance;

  factory CompareProvider() => _getInstance();

  static CompareProvider _getInstance() {
    if (_instance == null) {
      _instance = CompareProvider._();
    }
    return _instance;
  }

  //私有构造
  CompareProvider._();

  static const String _table = 'ta_compare';

  String createSql = 'CREATE TABLE $_table (_id INTEGER PRIMARY KEY,'
      ' $_compareTime TEXT, '
      ' $_deviceNo TEXT, '
      ' $_errorCode INTEGER, '
      ' $_faceUrl TEXT, '
      ' $_cropUrl TEXT, '
      ' $_score REAL, '
      ' $_style TEXT, '
      ' $_size INTEGER, '
      '$_staff_name TEXT)';

  insert(Compare compare) async {
    var _database = await SqfliteHelper().db;
    _database.insert(_table, compare.toMap());
  }

  Future delete(String deviceNo) async {
    var _database = await SqfliteHelper().db;
    return await _database.delete(
      _table,
      where: '$_deviceNo=?',
      whereArgs: [deviceNo],
    );
  }

  Future deleteByStyle(String style) async {
    var _database = await SqfliteHelper().db;
    return await _database.delete(
      _table,
      where: '$_style=?',
      whereArgs: [style],
    );
  }
}
